package com.peng.dian.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocalResolver implements LocaleResolver {
    //解析请求  下面的代码写完成以后放到bena容器中  写在自定义的MyMvcConfig中
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        //获取请求中的语言参数
        String language = httpServletRequest.getParameter("l");
        Locale locale= Locale.getDefault();//如果没有就使用默认的
        //如果请求的连接携带了地区化的参数
        if (!StringUtils.isEmpty(language)){
            //zh_CN en_Us 国家地区
            String[] split = language.split("_");
            locale = new Locale(split[0], split[1]);

        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
